package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoAsyncService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CartInfoAsyncServiceImpl implements CartInfoAsyncService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    //修改
    @Override
    @Async//异步请求
    public void updata(CartInfo cartInfo) {
        //cartInfoMapper.updateById(cartInfo);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",cartInfo.getUserId())
                .eq("sku_id",cartInfo.getSkuId()));
        System.out.println("线程2"+Thread.currentThread().getName());
    }
    //添加
    @Override
    @Async
    public void inseter(CartInfo cartInfo) {
        cartInfoMapper.insert(cartInfo);
        System.out.println("线程3"+Thread.currentThread().getName());
    }

    @Override
    @Async
    public void updataAsyncCartUserId(String userId, String userTempId) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>().eq("user_id",userTempId));
    }

    @Override
    @Async
    public void updataByIdNum(String userId, Long skuId, int skuNum) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setSkuNum(skuNum);
        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id",userId);
        cartInfoQueryWrapper.eq("sku_id",skuId);
        cartInfoMapper.update(cartInfo,cartInfoQueryWrapper);
    }

    @Override
    @Async
    public void delectUserTemIdCartInfo(String userTempId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().eq("user_id",userTempId));
    }

    @Override
    @Async
    public void updateByIdAsyncExt(String userTempId, String userId) {
        System.out.println("Async:updateByIdAsyncExt:" + Thread.currentThread().getName());
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",userTempId));
    }

    @Override
    @Async
    public void insertAsync(CartInfo cartInfo) {
        System.out.println("Async:" + Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);
    }

    //删除
    @Override
    @Async
    public void deleteByUserTempId(String userTempId) {
        System.out.println("Async:deleteByUserTempId:" + Thread.currentThread().getName());
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
                .eq("user_id",userTempId));
    }

    //Mysql更新购物车   打异步注解的方式   异步保存  异步调用
    @Override
    @Async  //不生效
    public void updateByIdAsync(CartInfo cartInfo) {

        System.out.println("Async:" + Thread.currentThread().getName());
        //cartInfoMapper.updateById(cartInfo);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",cartInfo.getUserId())
                .eq("sku_id",cartInfo.getSkuId()));

    }
}
